<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>发件箱</title>
		<link type="text/css" rel="stylesheet" href="../jquery-easyui-1.4.2/themes/default/easyui.css">
		<link type="text/css" rel="stylesheet" href="../jquery-easyui-1.4.2/themes/icon.css">
		<link rel="stylesheet" href="../ueditor/themes/default/_css/umeditor.css" />
		<script type="text/javascript" src="../js/jquery-2.1.1.min.js"></script>
		<script type="text/javascript" src="../jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../ueditor/umeditor.config.js" ></script>
		<script type="text/javascript" src="../ueditor/umeditor.min.js" ></script>
		<script type="text/javascript" src="../jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
		
		<style type="text/css">
			.td_text{
				text-align: right;
				padding-top: 5px;
			}
			.td_input{
				padding-left:15px ;
				padding-top: 8px;
			}
			#outBox_span{
				font-weight: bold;
				font-size: 14px;
				cursor:default;
				text-decoration: none;
			}
			#div_query{
				margin:20px 15px 10px;
			}
			#div_a a{
				display: block;
				margin-right: 20px;
				float:right;
			}
			#div_a{
				margin-right: 20px;
				padding-bottom:20px ;
			}
			.datagrid-header-row td{
   		 		background-color:#59b8e7;
   		 		font-weight:bold;
   		 		color:#fff;
   		 	}
		</style>
		<script type="text/javascript">
		$(function(){
			$('#win').window('close');
			var userId = $("#addresserId").val();
			$("#tableOutBox").datagrid({
				url:'../email/LoadDraftAndOutBox.do',
				pagination:true,
				queryParams:{pageNo:1,pageSize:10,addresserId:userId,draft:0},
				/*  ,keyword:"",direction:""*/
				rownumbers:true,
				striped:true,
				frozenColumns:[[{field:'hhhh',width:50,checkbox:true}]],
				columns:[[
				      {field:'mid',title:'编号',width:100,hidden:true},
				      {field:'title',title:'标题',width:100,align:'center',formatter:function(value,row,index){
				    	  return "<a href='javascript:lookMessage("+index+");'>"+value+"</a>";
				      }},
				      {field:'content',title:'内容',width:200,align:'center'},
				      {field:'addresser',title:'发件人',width:100,align:'center'},
				      {field:'recipients',title:'收件人',width:100,align:'center'},
				      {field:'stringDate',title:'编写日期',width:200,align:'center'}
				]]
			});
			var page = $("#tableOutBox").datagrid("getPager");
			page.pagination({
				onSelectPage:function(pageNumber,pageSize){
					$("#tableOutBox").datagrid('load',{pageNo:pageNumber,pageSize:pageSize,addresserId:userId,draft:0});
					page.pagination({
						pageNumber:pageNumber,
						pageSize:pageSize,
						addresserId:userId
						/* keyword:"",
						direction:"" */
					});
				}
			});
			//下拉框获取用户，填写到收件人文本框中
			$.post("../email/optionUser.do",function(data){
				$("#select_user").combobox({
					editable:false,//是否可编辑
					data:data,//把后台数据放入数组中
					valueField:'userId',//把数据放入value中
					textField:'userName',//把数据放入Text中
					//利用选中事件改变文本框及下拉框的值
					onSelect:function(record){
						var curValue=$("#select_user").combobox('getValue');
						var curText=$("#select_user").combobox('getText');
						$("#recipients").textbox('setText',curText);
						if(curValue>0){
							$("#select_user").combobox('setText','选择用户');
						}
					}
				});
			},"json");
		});
		function searchOutBox(){
			var userId = $("#addresserId").val();
			var keyword =$("#keyword").combobox("getText");
			var direction = $("#direction").combobox("getText");
			$("#tableOutBox").datagrid("load",{pageNo:1,pageSize:10,addresserId:userId,draft:0,keyword:keyword,direction:direction});
		}
		function deleteOutBox(){
			var selections = $("#tableOutBox").datagrid("getSelections");
			if(selections.length == 0){
				$.messager.alert("错误","请选中要删除的信息！","error");
				return;
			}
			var temp = new Array();
			var i = 0;
			for(i;i<selections.length;i++){
				temp.push(selections[i].mid);
			}
			 $.messager.confirm('确认信息','您确定要测底删除'+i+'条发件箱数据？',function(r){
				if(r){
					  $.post("../email/deleteById.do",{mid:temp},function(data){
					    	$('#tableOutBox').datagrid("reload");
					});  
				}  
			}); 
		}
		function insertRecycleStand(){
			var selections = $("#tableOutBox").datagrid("getSelections");
			if(selections.length == 0){
				$.messager.alert("错误","请选中要放入回收站的信息！","error");
				return;
			}
			var temp = new Array();
			var i = 0;
			for(i;i<selections.length;i++){
				temp.push(selections[i].mid);
			}
			 $.messager.alert('确认信息','您放入了'+i+'条数据到回收站！',"info",function(){
				  $.post("../email/addRecycleStand.do",{mid:temp},function(data){
				    	$('#tableOutBox').datagrid("reload");
				});  
			}); 
		}
		//发送信息
		function transpondMessage(){
			var title=$("#titleId").combobox('getText');
			var content=UM.getEditor('editor').body.innerHTML;
			var addresserId = $("#addresserId").val();
			var addresser = $("#userNameId").val();
			var recipients=$("#recipients").combobox('getText');
			if(addresserId == "" && addresser == ""){
				$.messager.alert("错误信息","您还没有登录！","error");
				return;
			}
			if(recipients == ""){
				$.messager.alert("错误信息","收件人不能为空！","error");
				return;
			}
			if(title == ""){
				$.messager.alert("错误信息","标题不能为空！","error");
				return;
			}
			if(content == ""){
				$.messager.alert("错误信息","内容不能为空！","error");
				return;
			}
			$.post("<%=basePath%>email/draftAndOutBox.do",{
				draft:0,
				title:title,
				content:content,
				addresserId:addresserId,
				addresser:addresser,
				recipients:recipients
			},function(data){
				if(data == "success"){
					$.messager.alert("确认信息","信息已发送！","info",function(){
						$('#tableOutBox').datagrid("reload");
						$('#win').window('close');
					});
				}else if(data == "error"){
					$.messager.alert("错误信息","收件人姓名不存在！","error");
				}
			},"text");
		};
		//把数据填到弹框中
		function lookMessage(index){
			//清除所有勾选的行
			$("#tableOutBox").datagrid("clearChecked");
			//勾选一行，行索引从0开始
			$("#tableOutBox").datagrid("checkRow",index);
			var selections = $("#tableOutBox").datagrid("getSelections");
			//表单回填
			var mid = selections[0].mid;
			$.post("<%=basePath%>email/inBoxById.do",{mid:mid,state:-1},function(data){
				$("#recipients").textbox('setText',"");
				$("#win").window('open');
				ue.setContent(data.content);
				$("#addresserLabel").text(data.addresser);
				$("#titleId").combobox("setText",data.title);
				$('#tableOutBox').datagrid("reload");
			},"json");
		};
		</script>
	</head>
	<body class="easyui-layout" style="margin: auto;">
		<div data-options="region:'north'" style="height:33px;padding:5px 0px 0px 5px">
			<span id="outBox_span">
				<span class="easyui-linkbutton" plain="true" iconCls="icon-outbox"></span>发件箱
			</span>
		</div>
		<div data-options="region:'center'">
		<input id="addresserId" type="hidden" value="${user.userId}">
		<input id="userNameId" type="hidden" value="${user.userName}">
			<div id="div_query">
				<span>关键字:</span>
				<input id="keyword" class="easyui-textbox" style="width:15%">
				<span>收件人:</span>
				<input id="direction" class="easyui-textbox" style="width:15%">
				<a href="javascript:searchOutBox();" class="easyui-linkbutton" iconCls="icon-search">查询</a>
			</div>
			<div id="div_a">
				<a href="javascript:deleteOutBox();">测底删除</a>
				<a href="javascript:insertRecycleStand();">放回收站</a>
			</div>
			<table id="tableOutBox" class="easyui-datagrid" style="width:98%;">
				
			</table>
			<!-- 查看信息弹框 -->
			<div id="win" class="easyui-window" title="查看信息" style="width:750px;height:450px"   
        	data-options="iconCls:'icon-add',modal:true,collapsible:false,minimizable:false,maximizable:false">   
	   	 		<div class="easyui-layout"style="width:100%;height:400px;margin-top:-20px;">
					<div data-options="region:'window'">
						<table id="tableId" style="width: 95%;margin-left: 35px;margin-top: 25px;">
							<tr>
								<td class="td_text">发件人:</td>
								<td class="td_input">
									<label id="addresserLabel"></label>
								</td>
							</tr>
							<tr>
								<td class="td_text">转发人:</td>
								<td class="td_input"><input id="recipients" class="easyui-textbox" data-options="prompt:'转发人姓名'" style="width:70%">
									<select id="select_user" class="easyui-combobox" style="width:auto;"data-options="required:true,panelHeight:'auto'">   
				   						<option>选择用户</option>     
									</select>  
								</td>
							</tr>
							<tr>
								<td class="td_text">标题:</td>
								<td class="td_input"><input id="titleId" name="title" class="easyui-textbox"style="width:80%"></td>
							</tr>
							<tr>
								<td class="td_text">内容:</td>
								<td class="td_input">
									<textarea id="editor" name="content" readonly="readonly" style="width: 600px;height: 160px;"></textarea>
									<script type="text/javascript">
										//渲染编辑器
							    		var ue = UM.getEditor('editor');
							    		/* var ready = false;
							    		ue.addListener("ready",function(){
							    			ready=true;
							    		}); */
									</script>
								</td>
							</tr>
							<tr>
								<td class="td_text">附件:</td>
								<td class="td_input"><input  class="easyui-filebox" name="file2" data-options="prompt:'点击右侧按钮选择文件',buttonText:'选择文件'" style="width:40%">(附件最多20M)</td>
							</tr>
						</table>
					</div>
	   	 		</div>
	   	 		<div style="text-align: center">
	   	 			<a id="btn" href="javascript:transpondMessage();" id="transpond" class="easyui-linkbutton" data-options="iconCls:'icon-add'">转发</a> 
	   	 			<a id="btn" href="javascript:void($('#win').window('close'));" class="easyui-linkbutton" data-options="iconCls:'icon-undo'">取消</a> 
	   	 		</div>
			</div>
		</div>
	</body>
</html>
